<template>
  <div id="apptb">
    <div class="grouptitle">
      <div class="grouptitletext">单练人员报名信息</div>
    </div>

    <div class="contentall">
      <div class="contentallcontent">
        <div class="contentss">
          <div class="inputall">
            <div class="inputleft">
              <label for="">项目名称</label>
              <select
                id="continent"
                class="inputtextte"
                v-model="project.projectname"
              >
                <option value="项目1">项目1</option>
                <option value="项目2">项目2</option>
                <option value="项目3">项目3</option>
              </select>
            </div>
            <div class="inputright">
              <label for="">套路名称</label>
              <input
                type="text"
                class="inputtextte"
                id="xqdzipt"
                style="width: 208px"
                v-model="project.routinename"
              />
            </div>
          </div>

          <div class="inputall">
            <div class="csry">
              <i class="el-icon-circle-check"></i>
              <label for="">请选择参赛人员</label>
            </div>
          </div>

          <div class="inputall" id="participant">
            <div class="inputright">
              <div class="participant" v-for="item in stafflist" :key="item.id">
                <input
                  type="radio"
                  name="participant-group"
                  class="inputtextte"
                  id="xqdzipts"
                  :value="item.username"
                  @change="selectedParticipant"
                />
                <label style="text-align: left">{{ item.username }}</label>
              </div>
            </div>
          </div>

          <div class="inputall" id="ipumew">
            <div class="bottomimg" @click="submitshut">
              <i class="el-icon-circle-plus-outline iconsel"></i>
              <span>保存</span>
            </div>
            <div class="bottomimg" style="margin-left: 15px" @click="submitgo">
              <i class="el-icon-right iconsel"></i>

              <span>下一步</span>
            </div>
          </div>
        </div>

        <div class="bottombox"></div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import { mapState, mapMutations, mapActions } from "vuex";
export default {
  data: function () {
    return {
      project: {
        projectname: "",
        routinename: "",
        participant: "",
      },
      stafflist: [],

      updateId: 0, // 需要修改的id
      isupdateAdd: false,
    };
  },
  methods: {
    selectbtn() {},

    //保存并关闭本窗:
    submitshut() {
      // ：是否是非空字符串
      function isNonEmptyStringIfString(value) {
        return (
          typeof value !== "string" ||
          (typeof value === "string" && value.trim() !== "")
        );
      }
      if (Object.values(this.project).every(isNonEmptyStringIfString)) {
        if (this.isupdateAdd) {
          this.updategetbyid(this.updateId);
          this.$message({
            message: "修改成功",
            type: "success",
          });

          this.closeck();
        } else {
          this.addstaff();
          this.$store.commit("addData", this.project);
          this.$message({
            message: "添加成功",
            type: "success",
          });

          this.closeck();
          localStorage.setItem("updateOne", JSON.stringify(true));
        }
      } else {
        this.$message("请填写完整信息");
      }
    },

    submitgo() {
      function isNonEmptyStringIfString(value) {
        return (
          typeof value !== "string" ||
          (typeof value === "string" && value.trim() !== "")
        );
      }
      if (Object.values(this.project).every(isNonEmptyStringIfString)) {
        if (this.isupdateAdd) {
          this.updategetbyid(this.updateId);
          this.$message({
            message: "修改成功",
            type: "success",
          });
        } else {
          this.addstaff();
          this.$message({
            message: "添加成功",
            type: "success",
          });

          setTimeout(() => {
            this.$router.push({
              name: "printinvoice",
            });
          }, 1000);
        }
      } else {
        this.$message("请填写完整信息");
      }
    },

    closeck() {
      setTimeout(() => {
        window.close();
      }, 1000);
    },

    selectedParticipant(e) {
      this.project.participant = e.target.defaultValue;
    },

    // 新增 人员：
    addstaff() {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/project/add",
        data: this.project,
      }).then((res) => {});
    },

    // 修改 人员
    updategetbyid(id) {
      axios({
        method: "post",
        url: "http://localhost:3002/competition/project/update",
        data: {
          id: id,
          projectid: this.project.projectid,
          projectname: this.project.projectname,
          routinename: this.project.routinename,
          participant: this.project.participant,
        },
      }).then((res) => {
        this.getStaff();
      });
    },

    // 获取 单个人员信息
    getstaffone() {
      axios({
        url: "http://localhost:3002/competition/project/getbyId",
        method: "post",
        data: { id: this.updateId },
      }).then((res) => {
        this.project = res.data.list[0];
      });
    },

    // 获取人员信息:
    getStaff() {
      axios({
        url: "http://localhost:3002/competition/staff/list",
        method: "post",
        data: {},
      }).then((res) => {
        this.stafflist = res.data.list;
      });
    },
  },
  mounted() {
    // 获取 修改 按钮 传递 过来的id：
    var url = window.location.href;
    var params = new URLSearchParams(url.split("?")[1]);
    var id = params.get("id");
    this.updateId = id;
    if (id) {
      this.getstaffone();
      this.isupdateAdd = true;
    }
    this.getStaff();
  },
};
</script>

<style lang="less" scoped>
.csry {
  margin-left: 16px;
  .el-icon-circle-check {
    margin-right: 10px;
  }
}
.iconsel {
  margin-right: 10px;
  margin-top: 3px;
}
#ipumew {
  // margin-left: 120px !important;
  display: flex;
  justify-content: flex-end;

  .bottomimg {
    margin-right: 20px !important;
  }
}
#apptb {
  margin: auto;
  transform: translateY(130px);
  background-color: #7c7e7c;
  width: 60%;
  height: 55%;
  border-radius: 10px;

  .grouptitle {
    width: 100%;
    height: 35px;
    color: #fff;
    background-color: #7c7e7c;
    display: flex;
    justify-content: center; /* 水平居中 */
    align-items: center; /* 垂直居中 */
    border-radius: 10px;
  }
  .contentall {
    background-color: #7c7e7c;
    width: 100%;
    height: 92%;
    padding-top: 6px;
    border-radius: 10px;

    .contentallcontent {
      width: 98.5%;
      height: 97%;
      background-color: #edefe9;
      margin: 0 6px;
      border-radius: 10px;
      position: relative;
      .contentss {
        width: 98%;
        // height: 80%;
        margin: 0 10px;
        background-color: #f0f2f4;
        transform: translateY(3%);
        border: 1px solid rgb(206, 200, 200);
        // 每一行
        .inputall {
          background-color: #f0f2f4;
          margin: 0px 6px;
          height: 40px;
          font-size: 15px;
          border-bottom: 1px solid rgb(206, 200, 200);
          display: flex;
          align-items: center; /* 垂直居中 */
          select {
            height: 25px;
            width: 227px !important;
          }
          .bottomimg {
            display: flex;
            align-items: right; /* 垂直居中 */
            // transform: translateX(220%);
            background: linear-gradient(#f6f6f6, #e1e1e1);
            padding: 4px 10px;
            border-radius: 5px;
            border: 1px solid rgb(174, 171, 171);
            img {
              width: 20px;
              height: 20px;
            }
          }

          .inputleft {
            display: inline-block;
            vertical-align: top;
            padding: 0 6px;
            .xqdzipt {
              display: inline-block;
              width: 400px !important;
            }
            label {
              display: inline-block;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
          .inputright {
            display: inline-block;
            vertical-align: top;
            margin-left: 5px;
            label {
              display: inline-block;
              width: 80px;
              text-align: center;
            }
            .inputtextte {
              width: 220px;
            }
          }
        }
      }

      .bottombox {
        background-color: #eff2f5;
        width: 100%;
        height: 18px;
        position: absolute;
        bottom: 0;
      }
    }
  }
}

#xqdzipts {
  width: 30px !important;
}

.participant {
  display: inline-block;
}
#participant {
  height: 100px !important;
}
</style>